Public Class Connection

    Private Const DisconnectSql As String = "UPDATE Settings SET Online = 0"
    Private Const ConnectSql As String = "UPDATE Settings SET Online = 1"
    Private Const StatusSql As String = "SELECT Online FROM Settings"

    Private Sub New()

    End Sub

    Public Shared Sub Connect()

        Dim conn As New OleDb.OleDbConnection()
        conn.ConnectionString = My.Settings.PoserFilesConnectionString

        Try

            conn.Open()

            Dim connectionStatusCommand As New OleDb.OleDbCommand
            connectionStatusCommand.CommandType = CommandType.Text

            connectionStatusCommand.CommandText = ConnectSql
            connectionStatusCommand.Connection = conn

            connectionStatusCommand.ExecuteNonQuery()

        Catch ex As Exception
            'do nothing
        Finally
            If conn.State <> ConnectionState.Closed Then
                conn.Close()
            End If
        End Try

    End Sub

    Public Shared Sub Disconnect()

        Dim conn As New OleDb.OleDbConnection()
        conn.ConnectionString = My.Settings.PoserFilesConnectionString

        Try

            conn.Open()

            Dim connectionStatusCommand As New OleDb.OleDbCommand
            connectionStatusCommand.CommandType = CommandType.Text

            connectionStatusCommand.CommandText = DisconnectSql
            connectionStatusCommand.Connection = conn

            connectionStatusCommand.ExecuteNonQuery()

        Catch ex As Exception
            'do nothing
        Finally
            If conn.State <> ConnectionState.Closed Then
                conn.Close()
            End If
        End Try

    End Sub

    Public Shared Function IsConnected() As Boolean

        Dim connected As Boolean = False

        Dim conn As New OleDb.OleDbConnection

        Try

            conn.ConnectionString = My.Settings.PoserFilesConnectionString
            conn.Open()

            Dim comm As New OleDb.OleDbCommand
            comm.CommandType = CommandType.Text
            comm.CommandText = StatusSql
            comm.Connection = conn

            connected = CType(comm.ExecuteScalar, Boolean)

        Catch ex As Exception
            'do nothing
        Finally
            If conn.State <> ConnectionState.Closed Then
                conn.Close()
            End If
        End Try

        Return connected

    End Function

End Class
